package com.leetcode.p0;

import com.utils.DataUtils;

public class Test75 {

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public void sortColors(int[] nums) {
        int length = nums.length;
        int i = 0;
        int left = 0;
        int right = length - 1;

        while (i <= right && left < right) {
            while (left < right && nums[left] == 0) {
                left++;
                i++;
            }

            while (right > left && nums[right] == 2) {
                right--;
            }

            if (nums[i] == 0) {
                swap(nums, i, left);
                left++;
                continue;
            }

            if (nums[i] == 2) {
                swap(nums, i, right);
                right--;
                continue;
            }
            i++;
        }
    }

    public static void main(String[] args) {
        Test75 test = new Test75();
        int[] array = new int[]{2, 0, 2, 1, 1, 0};
//        int[] array = new int[]{2, 2, 1, 1};
//        int[] array = new int[]{2, 2, 1};
//        int[] array = new int[]{1, 0, 2,  0};
//        int[] array = new int[]{2, 1, 0};
//        int[] array = new int[]{2, 0};
        test.sortColors(array);
        DataUtils.printArray(array);
    }

}
